<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * X-Promotion library class
 *
 *
 */
class Xpromo_lib
{
	/**
	 * Constructor
	 *
	 * Load x-promotion model.
	 */
	public function __construct()
	{
		$this->CI = &get_instance();

		$this->CI->load->model('pf/xpromo/xpromo_info_model');
		$this->CI->load->model('pf/user/user_login_app_model');
	}

	/**
	 * Get active x-promotion list which is not contain played application and this one
	 *
	 * @param	integer	user id
	 * @param	integer application id
	 * @param	integer os
	 * @param	integer chcode
	 * @return	object	x-promotion objects
	 */
	public function get_active_xp($uid=0, $appid=0, $os=0, $chcode=0)
	{
		// Get all active x-promtions
		$xps = $this->CI->xpromo_info_model->get_actives($os, $chcode);
		
		// Get applicaitons user played
		$apps = $this->CI->user_login_app_model->get_apps($uid);
		
		$appids = array();
		foreach ($apps as $app)
			array_push($appids, $app['app_id']);
		
		$result = array();
		foreach($xps as $xp)
		{
			// should drop user played app and current app
			if(!in_array($xp['app_id'], $appids))
			{
				unset($xp['status']);
				unset($xp['intval']);
				$xp['pic1'] = $this->CI->config->item('res_domain').$xp['pic1'];
				$xp['pic2'] = $this->CI->config->item('res_domain').$xp['pic2'];
				$xp['pic3'] = $this->CI->config->item('res_domain').$xp['pic3'];
				array_push($result, $xp);
			}
		}
		
		return $result;
	}
}
